<template>
  <div class="nav-header">
    <div class="fold-menu">
      <template v-if="isFoldMenu">
        <el-icon :size="iconSize" @click="foldMenu"><expand /></el-icon>
      </template>
      <template v-else-if="!isFoldMenu">
        <el-icon :size="iconSize" @click="foldMenu"><fold /></el-icon>
      </template>
    </div>

    <div class="content">
      <div>
        <breadcrumb :breadcrumb="breadcrumb" />
      </div>
      <div class="user-info">
        <el-dropdown>
          <!-- <span class="el-dropdown-link">
            下拉菜单
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span> -->
          <span class="el-dropdown-link">
            <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
            <span>{{ username }}</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>黄金糕</el-dropdown-item>
              <el-dropdown-item>狮子头</el-dropdown-item>
              <el-dropdown-item>螺蛳粉</el-dropdown-item>
              <el-dropdown-item disabled>双皮奶</el-dropdown-item>
              <el-dropdown-item divided>上海糖宝</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref } from 'vue'
import { Edit, Expand, Fold } from '@element-plus/icons'
import { ElIcon, ElDropdown, ElDropdownMenu, ElDropdownItem, ElAvatar } from 'element-plus'
import { useStore } from '@/store'
import { Breadcrumb } from '@/base-ui/breadcrumb'
import { getMenuBreadcrumb } from '@/utils/map-menus'
import { useRoute } from 'vue-router'

export default defineComponent({
  components: { ElIcon, Edit, Expand, Fold, ElDropdown, ElDropdownMenu, ElDropdownItem, ElAvatar, Breadcrumb },
  emits: ['foldChange'],
  setup(props, { emit }) {
    const iconSize = ref(25)
    const isFoldMenu = ref(false)

    const foldMenu = () => {
      isFoldMenu.value = !isFoldMenu.value
      emit('foldChange', isFoldMenu.value)
    }

    const store = useStore()
    const username = computed(() => store.state.login.userInfo.name)

    // 面包屑
    const breadcrumb = computed(() => {
      const userMenus = store.state.login.userMenus
      const route = useRoute()
      const currentPath = route.path
      return getMenuBreadcrumb(userMenus, currentPath)
    })
    return { iconSize, isFoldMenu, foldMenu, username, breadcrumb }
  }
})
</script>
<style scoped lang="less">
.nav-header {
  display: flex;
  width: 100%;

  .fold-menu {
    cursor: pointer;
    align-self: center;
  }

  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex: 1;
    padding: 0 20px;
  }

  .user-info {
    cursor: pointer;
  }
}
</style>
